package com.tx.admin.util.common;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.springframework.core.ResolvableType;

public class GenericUtil {

	/**
	 * 获得制定类的泛型参数
	 * @param from 泛型子类
	 * @param to 泛型父类
	 * @param index 泛型参数下标
	 * @return
	 */
	public static Class<?> getGeneric(Class<?> from,Class<?> to,int... index){
		ResolvableType  rt=ResolvableType.forClass(from);
		return rt.as(to).getGeneric(index).resolve();  
	}
	
	//字段时泛型字段
	public static Class<?> getGeneric(Field field,int... index){
		//化为泛型集合
		ResolvableType resolvableType =ResolvableType.forField(field);
		return resolvableType.getGeneric(index).resolve();
	}
	
	public static Class<?> getFieldGeneric(Type fileType,int index){
		ParameterizedType pt=(ParameterizedType)fileType;
		//获取泛型参数
		return (Class<?>) pt.getActualTypeArguments()[index];
	}
}
